package app.server.project.main.service.controller;

import app.server.project.core.exception.SysError;
import app.server.project.core.http.RespModel;
import app.server.project.core.httpauth.UnAuth;
import app.server.project.core.token.TokenCache;
import app.server.project.core.util.AssertUtil;
import app.server.project.core.util.SysTimeUtil;
import app.server.project.main.service.dto.BotMsgReq;
import app.server.project.main.service.dto.auth.LoginResp;
import app.server.project.main.service.dto.auth.SynDto;
import app.server.project.main.service.dto.cms.ChangeTimeReq;
import app.server.project.main.service.service.BotPushService;
import app.server.project.main.service.service.auth.AuthService;
import app.server.project.main.service.service.cms.GMService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统服务模块
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sys")
public class SysController {

    private AuthService authService;
    private GMService gmService;
    private BotPushService botPushService;

    /**
     * 设置系统时间
     *
     * @param
     * @return
     */
    @PostMapping("/setTime")
    public RespModel invite(@RequestBody ChangeTimeReq time) {
        SysTimeUtil.setAppTime(time.getTime());
        return RespModel.success();
    }

    /**
     * 还原系统时间
     *
     * @param
     * @return
     */
    @PostMapping("/resetSysTime")
    public RespModel resetSysTime() {
        SysTimeUtil.reset();
        return RespModel.success();
    }

    @UnAuth
    @RequestMapping("/innerToken/{innerToken}")
    public LoginResp getInnerSysToken(@PathVariable String innerToken) {
        return this.authService.getInnerSysToken(innerToken);
    }

    @RequestMapping("/getTargetSysToken")
    public SynDto getTargetSysToken() {
        return this.authService.getTargetSysToken();
    }

    @UnAuth
    @PostMapping("/uploadImg/{userId}/{token}")
    public RespModel uploadImg(@RequestParam("Fdata") MultipartFile files, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.botPushService.uploadMsg(files);
        return RespModel.success();
    }

    @RequestMapping("/pushBotMsg")
    public RespModel pushBotMsg(@RequestBody BotMsgReq req) {
        this.botPushService.sendMsg(req.getMsg());
        return RespModel.success();
    }
}
